package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiWishRewardData;
import com.yanqu.road.entity.activity.youxing.ShanShuiUserWishReward;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouShanShuiWishRewardDataDaoImpl extends TempDataObjectDao<CrossXingYouShanShuiWishRewardData> {

    public static String getCrossXingYouShanShuiWishRewardDataSql = "select * from `t_c_xing_you_team_wish_data` where `activityId`=? and groupId = ?";
    public Map<Integer, List<CrossXingYouShanShuiWishRewardData>> getCrossXingYouShanShuiWishRewardData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<CrossXingYouShanShuiWishRewardData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouShanShuiWishRewardDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXingYouShanShuiWishRewardData data = parse(rs);
                if(!result.containsKey(data.getTeamId())){
                    result.put(data.getTeamId(), new ArrayList<>());
                }
                result.get(data.getTeamId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossXingYouShanShuiWishRewardDataSql = "update `t_c_xing_you_team_wish_data` set rewardMap=? where activityId=? and groupId = ? and teamId=? and round=?";
    @Override
    protected boolean updateData(CrossXingYouShanShuiWishRewardData crossXingYouShanShuiWishRewardData) {
        crossXingYouShanShuiWishRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiWishRewardData.getRewardMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiWishRewardData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiWishRewardData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiWishRewardData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiWishRewardData.getRound()));
        if (getDb().executeNoneQuery(updateCrossXingYouShanShuiWishRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossXingYouShanShuiWishRewardDataSql = "insert into `t_c_xing_you_team_wish_data`(activityId,groupId,teamId,round,rewardMap) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouShanShuiWishRewardData crossXingYouShanShuiWishRewardData) {
        crossXingYouShanShuiWishRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiWishRewardData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiWishRewardData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiWishRewardData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiWishRewardData.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiWishRewardData.getRewardMap())));
        if (getDb().executeNoneQuery(insertCrossXingYouShanShuiWishRewardDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouShanShuiWishRewardData parseData(ResultSet rs) throws SQLException {
        CrossXingYouShanShuiWishRewardData crossXingYouShanShuiWishRewardData = new CrossXingYouShanShuiWishRewardData();
        crossXingYouShanShuiWishRewardData.setActivityId(rs.getInt("activityId"));
        crossXingYouShanShuiWishRewardData.setTeamId(rs.getInt("teamId"));
        crossXingYouShanShuiWishRewardData.setRound(rs.getInt("round"));
        crossXingYouShanShuiWishRewardData.setRewardMap(JSON.parseObject(rs.getString("rewardMap"), new TypeReference<Map<Long, ShanShuiUserWishReward>>() {}));
        crossXingYouShanShuiWishRewardData.setGroupId(rs.getInt("groupId"));
        return crossXingYouShanShuiWishRewardData;
    }
}
